package revontuli.aMHD;

import java.util.ArrayList;

import revontuli.aMHD.R;
import revontuli.aMHD.dataclasses.Line;
import revontuli.aMHD.UIhelpers.LineListAdapter;
import android.app.TabActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.TabHost;

public class LineListAct extends TabActivity implements TabHost.OnTabChangeListener {
	private TabHost mTabHost;
	private ListView mListView;
	private LineListAdapter mListAdapter;
	private ArrayList<Line> mLines;
	
	UserLocationSvc mService;
	private ServiceConnection mConnection = new ServiceConnection() {
		@Override
		public void onServiceDisconnected(ComponentName name) {
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			UserLocationSvc.ClientBinder binder = (UserLocationSvc.ClientBinder) service;
			mService = binder.getService();
			mService.registerMessenger(null);
		}
	};
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mTabHost = getTabHost();
		LayoutInflater.from(this).inflate(R.layout.linelist, mTabHost.getTabContentView(), true);
		
		mTabHost.addTab(mTabHost.newTabSpec("busTab")
				.setIndicator(/*getResources().getString(R.string.buses)*/"", getResources().getDrawable(R.drawable.bus_icon))
				.setContent(R.id.LineListView));
		
		mTabHost.addTab(mTabHost.newTabSpec("tramTab")
				.setIndicator(/*getResources().getString(R.string.trams)*/"", getResources().getDrawable(R.drawable.tram_icon))
				.setContent(R.id.LineListView));
		
		mTabHost.addTab(mTabHost.newTabSpec("trolleyTab")
				.setIndicator(/*getResources().getString(R.string.trolleys)*/"", getResources().getDrawable(R.drawable.trolley_icon))
				.setContent(R.id.LineListView));
		
		mTabHost.addTab(mTabHost.newTabSpec("nightTab")
				.setIndicator(/*getResources().getString(R.string.nights)*/"", getResources().getDrawable(R.drawable.night_icon))
				.setContent(R.id.LineListView));
		
		mTabHost.addTab(mTabHost.newTabSpec("regionalTab")
				.setIndicator(/*getResources().getString(R.string.regionals)*/"", getResources().getDrawable(R.drawable.regional_icon))
				.setContent(R.id.LineListView));
		
		mTabHost.setOnTabChangedListener(this);
		mListView = (ListView) mTabHost.getTabContentView().findViewById(R.id.LineListView);
	}
	
	@Override
	public void onTabChanged(String tabId) {
		if (tabId == "busTab") {
			showBusLines();
		} else if (tabId == "tramTab") {
			showTramLines();
		} else if (tabId == "trolleyTab") {
			showTrolleyLines();
		} else if (tabId == "nightTab") {
			showNightLines();
		} else if (tabId == "regionalTab") {
			showRegionalLines();
		}
	}
	
	@Override
	public void onStart() {
		super.onStart();
		bindService(new Intent(this, UserLocationSvc.class), mConnection, Context.BIND_AUTO_CREATE);
	}
	
	@Override
	public void onStop() {
		super.onStop();
		if (mService != null) {
			unbindService(mConnection);
		}
	}
	
	//metody menu
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.linelistmenu, menu);
		return true;
	}
	
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		if (aMHDApp.mDataManager.getOffline()) { //offline mod
			//5-offline button, 6-online button
			menu.getItem(5).setVisible(false);
			menu.getItem(6).setVisible(true);
			menu.getItem(1).setEnabled(false);
			menu.getItem(2).setEnabled(false);
		} else { //online mod
			menu.getItem(5).setVisible(true);
			menu.getItem(6).setVisible(false);
			menu.getItem(1).setEnabled(true);
			menu.getItem(2).setEnabled(true);
		}
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.MenuLLFinder:
			startActivity(new Intent(getApplicationContext(), StopFinderAct.class));
			return true;
		case R.id.MenuLLMap:
			startActivity(new Intent(getApplicationContext(), StopMapAct.class));
			return true;
		case R.id.MenuLLConnection:
			startActivity(new Intent(getApplicationContext(), ConnFinderAct.class));
			return true;
		case R.id.MenuLLStopList:
			startActivity(new Intent(getApplicationContext(), StopListAct.class));
			return true;
		case R.id.MenuLLSettings:
			startActivity(new Intent(getApplicationContext(), SettingsAct.class));
			return true;
		case R.id.MenuLLOffline:
			aMHDApp.mDataManager.setOffline(true);
			return true;
		case R.id.MenuLLOnline:
			aMHDApp.mDataManager.setOffline(false);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	private void showBusLines() {
		mLines = aMHDApp.mDataManager.getAllLines(aMHDApp.VEHICLE_BUS);
		mListAdapter = new LineListAdapter(this, mLines);
		mListView.setAdapter(mListAdapter);
		mListAdapter.notifyDataSetChanged();
	}

	private void showTramLines() {
		mLines = aMHDApp.mDataManager.getAllLines(aMHDApp.VEHICLE_TRAM);
		mListAdapter = new LineListAdapter(this, mLines);
		mListView.setAdapter(mListAdapter);
		mListAdapter.notifyDataSetChanged();
	}
	
	private void showTrolleyLines() {
		mLines = aMHDApp.mDataManager.getAllLines(aMHDApp.VEHICLE_TROLLEY);
		mListAdapter = new LineListAdapter(this, mLines);
		mListView.setAdapter(mListAdapter);
		mListAdapter.notifyDataSetChanged();
	}
	
	private void showNightLines() {
		mLines = aMHDApp.mDataManager.getAllLines(aMHDApp.VEHICLE_NIGHT);
		mListAdapter = new LineListAdapter(this, mLines);
		mListView.setAdapter(mListAdapter);
		mListAdapter.notifyDataSetChanged();
	}
	
	private void showRegionalLines() {
		mLines = aMHDApp.mDataManager.getAllLines(aMHDApp.VEHICLE_REGIONAL);
		mListAdapter = new LineListAdapter(this, mLines);
		mListView.setAdapter(mListAdapter);
		mListAdapter.notifyDataSetChanged();
	}
}
